/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.digitalization.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.LogConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.contant.ConstantDigit;
import org.springblade.modules.digitalization.dto.DaLogDto;
import org.springblade.modules.digitalization.entity.A01Entity;
import org.springblade.modules.digitalization.excel.A01Excel;
import org.springblade.modules.digitalization.manager.LogManager;
import org.springblade.modules.digitalization.service.IA01Service;
import org.springblade.modules.digitalization.service.IA02Service;
import org.springblade.modules.digitalization.service.IDigitPinyinService;
import org.springblade.modules.digitalization.vo.A01VO;
import org.springblade.modules.digitalization.wrapper.A01Wrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 人员基本信息集 控制器
 *
 * @author BladeX
 * @since 2022-10-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("damanager/a01")
@Api(value = "人员基本信息集", tags = "人员基本信息集接口")
public class A01Controller extends BladeController {

	private final IA01Service a01Service;
	private final IA02Service a02Service;
	private final IDigitPinyinService pinyinService;
	private final LogManager logManager;

	/**
	 * 人员基本信息集 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入a01")
	public R<A01VO> detail(A01Entity a01) {
		A01Entity detail = a01Service.getOne(Condition.getQueryWrapper(a01));
		return R.data(A01Wrapper.build().entityVO(detail));
	}

	/**
	 * 人员基本信息集 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入a01")
	public R<IPage<A01VO>> list(A01Entity a01, Query query) {
		IPage<A01Entity> pages = a01Service.page(Condition.getPage(query), Condition.getQueryWrapper(a01));
		return R.data(A01Wrapper.build().pageVO(pages));
	}

	@GetMapping("/list/ids")
	public R<List<A01VO>> list(@RequestParam("ids") String ids) {
		List<A01Entity> byA0000 = a01Service.getByA0000(Func.toStrList(ids));
		return R.data(A01Wrapper.build().listVO(byA0000));
	}

	/**
	 * 人员基本信息集 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入a01")
	public R<IPage<A01VO>> page(A01VO a01, Query query) {
		IPage<A01VO> pages = a01Service.selectA01Page(Condition.getPage(query), a01);
		return R.data(pages);
	}

	/**
	 * 人员基本信息集 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入a01")
	public R save(@Valid @RequestBody A01Entity a01) {
		boolean save = a01Service.save(a01);
		logManager.doAdd(a01.getA0000(), LogManager.TYPE_A01, a01);
		return R.status(save);
	}

	/**
	 * 人员基本信息集 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入a01")
	public R update(@Valid @RequestBody A01Entity a01) {
		A01Entity oldData = a01Service.getById(a01.getId());
		boolean b = a01Service.updateById(a01);
		logManager.doUpdate(a01.getA0000(), LogManager.TYPE_A01, oldData, a01);
		return R.status(b);
	}

	/**
	 * 人员基本信息集 修改
	 */
	@ApiLog("贴码")
	@GetMapping("/updateToday")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "更新当日码状态", notes = "传入a01")
	public R<Boolean> updateToday() {
		DateTime dateTime = cn.hutool.core.date.DateUtil.beginOfDay(new Date());
		List<A01Entity> list = a01Service.list(Wrappers.<A01Entity>lambdaUpdate().gt(A01Entity::getCreateTime, dateTime).eq(A01Entity::getIsDeleted, 0).eq(A01Entity::getStatus, ConstantDigit.A01_STATUS_USER));
		boolean update = a01Service.update(Wrappers.<A01Entity>lambdaUpdate().gt(A01Entity::getCreateTime, dateTime).eq(A01Entity::getIsDeleted, 0).eq(A01Entity::getStatus, ConstantDigit.A01_STATUS_USER).set(A01Entity::getStatus, ConstantDigit.A01_STATUS_ERCODE));
		list.parallelStream().forEach(oldData -> {
			A01Entity newData = ObjectUtil.clone(oldData);
			newData.setStatus(ConstantDigit.A01_STATUS_ERCODE);
			logManager.doBizUpdate(oldData.getA0000(), LogManager.TYPE_A01, "贴码", oldData, newData);
		});
		return R.data(update);
	}

	/**
	 * 人员基本信息集 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入a01")
	public R submit(@Valid @RequestBody A01Entity a01) {
		Boolean aBoolean = a01Service.saveOrUpdateA01(a01);
		return R.status(aBoolean);
	}

	@ApiLog("目录")
	@PostMapping("/menuOpt")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入a01")
	public R menuOpt(@Valid @RequestBody A01Entity a01) {

		A01Entity oldData = a01Service.getById(a01.getId());

		Boolean aBoolean = a01Service.saveOrUpdateA01(a01);

		logManager.doBizUpdate(a01.getA0000(), LogManager.TYPE_A01, "目录完成", oldData, a01);

		return R.status(aBoolean);
	}

	/**
	 * 人员基本信息集 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		boolean b = a01Service.deleteByIds(Func.toLongList(ids));
		return R.status(b);
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/export-a01")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入a01")
	public void exportA01(@ApiIgnore @RequestParam Map<String, Object> a01, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<A01Entity> queryWrapper = Condition.getQueryWrapper(a01, A01Entity.class);
		queryWrapper.lambda().eq(A01Entity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<A01Excel> list = a01Service.exportA01(queryWrapper);
		ExcelUtil.export(response, "人员基本信息集数据" + DateUtil.time(), "人员基本信息集数据表", list, A01Excel.class);
	}


}
